var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            ID: 0,
            pageindex: 0,
            pagesize: 10,
            isviewdetail: true,
            isFirstLoad: 1
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        number: 0,
        checked: false,
        Read: false,
        chooseId: 0,//选择的工单id
        showWin: false,
        isShowRead: false,
        touchID: 0,//长按选中id
    },
    methods: {
        get_data: function () {
            var that = this;
            that.can_scroll = false;
            var options = {};
            options.action = "getdeviceservicejpushlist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.ReadStatus = that.Read ? 1 : 0;
            if (that.form.pageindex > 0) {
                toast.loading({
                    title: "加载中",
                    duration: 2000
                }, function (ret) { });
            }
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;
                    } else {
                        that.list = that.list.concat(data.list);
                    }
                    app.current_item_length = that.list.length
                    that.number=data.number;
                    for(var i=0;i<data.list.length;i++){
                        data.list[i].JpushAddTime=ns.SetTime_style(data.list[i].JpushAddTime);
                    }

                    if(that.form.ID){
                        setTimeout(function() {
                            that.do_scrollTo();
                        }, 1000)
                    }

                    api.sendEvent({
                        name:'home_new_updata'
                    })
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        choose_noread: function () {
            var that = this;
            that.Read = !that.Read;
            that.get_data();
        },
        do_Read: function () {
            var that = this;
            var options = {};
            options.action = "jpushlogusersetread";
            options.JpushLogID = that.touchID;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.showWin = false;
                    that.get_data();
                }
            });
        },
        //删除消息
        deleteItem: function () {
            var that = this;
            var options = {};
            options.action = "doremovepushmsg";
            options.ID = that.touchID;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.showWin = false;
                    that.get_data();
                }
            });
        },
        All_read: function () {
            var that=this;
            var options={};
            options.action="jpushlogallread";
            options.Type=24;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.get_data();
                }
            });
        },
        //点击遮罩关闭弹窗
        doHideWin: function () {
            var that = this;
            that.showWin = false;
            that.touchID = 0;
            that.get_data();
        },
        //长按弹出操作框
        gettouchStart: function (index, JpushLogID, IsRead) {
            var that = this;
            api.addEventListener({
                name: 'longpress'
            }, function (ret) {
                that.showWin = !that.showWin;
                if (!IsRead) {
                    that.isShowRead = true;
                } else {
                    that.isShowRead = false;
                }
                that.touchID = JpushLogID;
            })
        },
        openGongdan: function (item) {
            var that = this;
            var options = {};
            options.action = "jpushlogusersetread";
            options.JpushLogID = item.JpushLogID;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.get_data();
                }
            });
            ns.openWin('repairdetail_frm', '工单详情', {
                id: item.ID,
                isviewdetail: that.form.isviewdetail
            });
        },
        open_renwu: function () {
            // ns.openWin('repairlist_renwulist', '维修工单待办');
        },
        //翻页第一条数据的位置
        do_scrollbtn: function (id) {
            var sollid = document.getElementById('scroll' + id);
            window.scrollTo(0, sollid.offsetTop);
        },
        //搜索结果返回位置
        do_scrollTo: function () {
            var that = this;
            var sollid = document.getElementById('scroll' + that.form.ID);
            window.scrollTo(0, sollid.offsetTop);
            var classlist = document.getElementById('scroll' + that.form.ID).getElementsByClassName('boxbg')[0];
            classlist.style.backgroundColor = "#eaf1ff";
            setTimeout(function () {
                classlist.style.backgroundColor = "#f7fbff";
            }, 800)

        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.ID = api.pageParam.ID || 0;
    dialog = new auiDialog();
    toast = new auiToast();

     //清空
     api.addEventListener({
        name: 'do_open_delete'
    }, function (ret, err) {
        api.confirm({
            title: '提示信息',
            msg: '确认要清理所有的消息信息吗？',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            if (ret) {
                if (ret.buttonIndex == 1) {
                    var options={};
                    options.action="doremoveallpushmsg";
                    options.Type=24;//1-安防巡更，2-绿化保洁 24-维修工单
                    ns.post(options, function(succeed, data, err) {
                        if (succeed) {
                            app.get_data();
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '删除中'
                    });
                }
            }
        })
    });
    ns.init_pull();
    app.get_data();
};
